package com.lam.common.components.easyexcel.imp.model;

import com.lam.common.exception.BaseException;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.ImportAdd;
import com.lam.common.validate.group.ImportUpdate;

/**
 * @author lam
 *
 * @param <Ie> 用于接收数据对象的Java类
 * @param <R> 用于保存数据对象的Java类
 */
public abstract class AbstractImportBody<Ie, R> implements ImportBody<Ie, R> {

	@Override
	public String insertRecord(ImportEntry<Ie, R> entry) {
		String msg = ValidateUtil.validate(entry.getData(), ImportAdd.class);
		if(StringUtils.isBlank(msg)) { insertData(entry.getData()); }
		return msg;
	}
	
	public abstract int insertData(R data);
	
	public int updateData(R data) {
		throw new BaseException("该导入操作不支持修改数据！");
	}
	
	@Override
	public String updateRecord(ImportEntry<Ie, R> entry) {
		String msg = ValidateUtil.validate(entry.getData(), ImportUpdate.class);
		if(StringUtils.isBlank(msg)) { updateData(entry.getData()); }
		return msg;
	}
}
